package cn.edu.scau.cmi.zengGuanSheng.hibernate.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

import cn.edu.scau.cmi.zengGuanSheng.hibernate.domain.Teacher;

public class TeacherDao extends BaseHibernateDao {
	
	//添加教师
	public static void addTeacher(Teacher t) {
		Session s = getSession();
		s.save(t);
		commitTransactionAndClose(s);
	}
	
	//删除教师
	public static Teacher deleteTeacher(Teacher t) {
		Session s = getSession();
		Teacher t1 = (Teacher)s.get(Teacher.class, t.getId());
		s.delete(t1);
		commitTransactionAndClose(s);
		return t1;
	}
	
	/**
	 * 根据教师的姓名查询教师
	 * @param t
	 * @return
	 */
	public static Teacher findTeacherByName(Teacher t) {
		Session s = getSession();
		Query q =s.createQuery("from Teacher t where t.name = ?");
        q.setString(0, t.getName());
        List<Teacher> list= q.list();
        commitTransactionAndClose(s);
        return list.get(0);
	}
	
	//根据教师学号获取教师
	public static Teacher findTeacherByNo(Teacher t) {
		Session s = getSession();
		Teacher t1 = (Teacher)s.get(Teacher.class, t.getId());
		commitTransactionAndClose(s);
		return t1;
	}
	
	//保存修改的教师信息
	public static void saveEdit(Teacher t) {
		Session s = getSession();
		s.update(t);
		commitTransactionAndClose(s);
	}
	
}
